package cn.hh.dictionary.controller;


import cn.hh.basic.util.AjaxResult;
import cn.hh.basic.util.PageList;
import cn.hh.dictionary.domain.Detail;
import cn.hh.dictionary.query.DetailQuery;
import cn.hh.dictionary.service.IDetailService;
import cn.hh.org.query.EmpQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/detail")
public class DetailController {
    @Autowired
    private IDetailService detailService;
    /*编辑*/
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Detail detail){
        /*通过id判断是新增还是删除*/
        try {
            if (detail.getId()==null){
                detailService.add(detail);
            }else {
                detailService.update(detail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            /*反馈操作失败，及具体错误*/
            AjaxResult ajaxResult = AjaxResult.me().setMessage("操作失败" + e.getMessage());
            return ajaxResult;
        }
    }
    /*删除*/
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable("id")Long id){
        try {
            detailService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            /*反馈操作失败，及具体错误*/
            AjaxResult ajaxResult = AjaxResult.me().setMessage("操作失败" + e.getMessage());
            return ajaxResult;
        }
    }
    /*批量删除*/
    @PatchMapping
    public AjaxResult deleteByIds(@RequestBody Long[] ids){
        try {
            detailService.deleteByIds(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            /*反馈操作失败，及具体错误*/
            AjaxResult ajaxResult = AjaxResult.me().setMessage("操作失败" + e.getMessage());
            return ajaxResult;
        }
    }
    /*查询所有*/
    @GetMapping
    public List<Detail> loadAll(){
        return detailService.loadAll();
    }
    /*分页*/
    @PostMapping
    public PageList<Detail> queryPage(@RequestBody DetailQuery query){
        return detailService.queryPage(query);
    }
}
